package com.aaa.service.impl;


import com.aaa.dao.MenuDao;
import com.aaa.entity.Menu;
import com.aaa.service.MenuService;
import com.aaa.util.*;
import com.aaa.vo.AddOrUpdMenu;
import com.aaa.vo.MenuVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findByUserid(Long userid) {

        return menuDao.selectByUserid(userid);
    }

    @Override
    public Result<List<Menu>> getLeftMenu() {
        String token = WebUtil.getRequest().getHeader("token");
//        System.out.println("------------------------------ " + token);
        Map<String, Object> tokenInfo = JwtUtil.getTokenChaim(token);
        String phone = (String) tokenInfo.get("username");
        System.out.println("当前用户------------------------ = " + phone);
        //查询用户所拥有的菜单
        List<Menu> menus =menuDao.findLeftMenu(phone);
        //一级菜单
        List<Menu> collect = menus.stream().filter(ele -> ele.getParentId() == 0).collect(Collectors.toList());
        //二级菜单......
        for (Menu one : collect) {
            one.setChildrenMenu(getChildren(one.getMenuId(), menus));
        }

//        System.out.println("当前用户拥有的菜单------------- " + collect);
        return new Result<>(2000,"ok",collect);
    }

    @Override
    public Result<List<Integer>> findByRoleId(Long roleId) {
        List<Integer> menuIds= menuDao.getByRoleId(roleId);
        return new Result<>(2000,"查询选中的菜单id",menuIds);

    }

    @Override
    public Result<List<Menu>> getAll(MenuVo menuVo) {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        if (menuVo.getStatus()!=null){
            wrapper.eq("status",menuVo.getStatus());
        }
        //null "" "  "
        if (StringUtils.hasText(menuVo.getMenuName())){
            wrapper.like("menu_name",menuVo.getMenuName());
        }
        List<Menu> menus = menuDao.selectList(wrapper);
        //1.查询所有的一级菜单
        List<Menu> firsts=menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
        //2.查询一级菜单对应的二级菜单---只适合只有两级菜单
//        System.out.println("一级菜单 = " + firsts);
        for(Menu f:firsts){
            f.setChildrenMenu(getChildren(f.getMenuId(),menus));
        }
//        System.out.println("二级菜单 = " + firsts);
        return new Result<>(2000,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Menu>> getAll() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Menu> menus = menuDao.selectList(wrapper);
        //1.查询所有的一级菜单
        List<Menu> firsts=menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
        //2.查询一级菜单对应的二级菜单---只适合只有两级菜单
//        System.out.println("一级菜单 = " + firsts);
        for(Menu f:firsts){
            f.setChildrenMenu(getChildren(f.getMenuId(),menus));
        }
//        System.out.println("二级菜单 = " + firsts);
        return new Result<>(2000,"查询菜单成功",firsts);

    }

    @Override
    public Result<List<Menu>> getMenuType() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("parent_id",0);
        List<Menu> menus = menuDao.selectList(wrapper);

        return new Result<List<Menu>>().setData(menus);
    }

    @Autowired
    private AccountName accountName;

    //添加或修改菜单
    @Override
    public Result<String> addOrUpdMenu(AddOrUpdMenu addOrUpdMenu) {
        String userName = accountName.getAccountName();
        //token获取用户
        int num=0;
        //添加
        Menu addMenu = getOwnUpdOrAdd(addOrUpdMenu);
        if (addOrUpdMenu.getMenuId()==null){
            addMenu.setCreateBy(userName);
            addMenu.setCreateTime(new Date());
            System.out.println("添加=========== = " + addMenu);
            num = menuDao.insert(addMenu);
        }else {
            addMenu.setMenuId(addOrUpdMenu.getMenuId());
            addMenu.setUpdateBy(userName);
            addMenu.setUpdateTime(new Date());
            System.out.println("修改=========== = " + addMenu);
            num = menuDao.updateById(addMenu);
        }
        System.out.println("num = " + num);
        if (num>0){
            return Result.success();
        }else {
            return Result.error();
        }

    }

    private Menu getOwnUpdOrAdd(AddOrUpdMenu addOrUpdMenu) {
        Menu addMenu = new Menu();
        addMenu.setParentId(addOrUpdMenu.getParentId());
        addMenu.setParentIds(String.valueOf(addOrUpdMenu.getParentId()));
        addMenu.setMenuName(addOrUpdMenu.getMenuName());
        addMenu.setPath(addOrUpdMenu.getPath());
        addMenu.setMenuType(addOrUpdMenu.getMenuType());
        addMenu.setRemark(addOrUpdMenu.getRemark());
        addMenu.setStatus(addOrUpdMenu.getStatus());
        return addMenu;
    }

    @Override
    public Result<Menu> getMenuById(Long id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",id);
        Menu menu = menuDao.selectOne(queryWrapper);
        return new Result<>(2000,"ok",menu) ;
    }

    @Override
    public Result<String> delByMenuId(Long menuId) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuId);
        int delete = menuDao.delete(queryWrapper);
        if (delete>0){
            return Result.success();
        }
        return Result.error();
    }


    @Override
    public Result<String> confirmMenu(Long roleId, Long[] menuIds) {
        //roleId删除具有的菜单id
        menuDao.deleteByRoleId(roleId);
        //批量添加
        Integer integer = menuDao.batchMenus(roleId, menuIds);
        if (integer>0){
            return new Result<>(2000,"分配成功");
        }else {
            return Result.error();
        }

    }


//    private List<Menu> getChildren(Long parentId, List<Menu> menus){
//        ArrayList<Menu> two = new ArrayList<>();
//        for (Menu menu : menus) {
//            if (menu.getParentId().equals(parentId)){
//                two.add(menu);
//            }
//        }
//
//        for (Menu n : two) {
//            n.setChildrenMenu(getChildren(n.getMenuId(),two));
//        }
//
//        return two;
//    }


    private List<Menu> getChildren(Long parentId,List<Menu> menus){
        List<Menu> seconds=new ArrayList<>();
        for(Menu m:menus){
            if(m.getParentId().equals(parentId)){
                seconds.add(m);
            }
        }
        for(Menu m:seconds){
            m.setChildrenMenu(getChildren(m.getMenuId(),menus));
        }
        return seconds;
    }
}






















